package com.meiyu.live.ui.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.android.volley.Response;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.MyhookupResponse;
import com.meiyu.live.popj.Hookup;
import com.meiyu.live.ui.main.AnchorDetailActivity;
import com.meiyu.live.ui.main.widget.MyHookItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的直播
 */
public class MyTvActivity extends BaseActivity implements View.OnClickListener {


    private LayoutInflater inflater;
    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private DataListAdapter adapter;
    private View btn_back, footerView;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private static final int PAGE_SIZE = 12;
    private int index = 1;
    private boolean hasMore = true;
    private MyhookupResponse myhookupResponse = new MyhookupResponse();
    private List<Hookup> data = new ArrayList<Hookup>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mytv);
        init();
    }

    protected void init() {
        inflater = LayoutInflater.from(this);
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        listView = (ListView) findViewById(R.id.listview);

        footerView = inflater.inflate(R.layout.view_loadmore, null);
        listView.addFooterView(footerView);
        footerView.setVisibility(View.GONE);

        adapter = new DataListAdapter();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { // 判断滚动到底部
                    if (absListView.getLastVisiblePosition() == (absListView.getCount() - 1) && hasMore) {
                        footerView.setVisibility(View.VISIBLE);
                        getData(false);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });

        getData(true);
    }

    private void getData(final boolean reload) {
        if (reload) {
            index = 1;
            hasMore = true;
        }
        int area[] = {PAGE_SIZE, index + PAGE_SIZE - 1};
        BaseBodyParams b = new BaseBodyParams();
        b.setUrl(Constant.VERSION + Constant.UrlParams.MYHOOPUP);
        request(b, MyhookupResponse.class, new Response.Listener<MyhookupResponse>() {
            @Override
            public void onResponse(MyhookupResponse response) {
                myhookupResponse = response;
                refreshLayout.setRefreshing(false);
                footerView.setVisibility(View.GONE);
                if (response != null) {
                    if (reload) {
                        data.clear();
                    }
                    data.addAll(response.getBody());
                    index += PAGE_SIZE;
                    if (response.getBody().size() < PAGE_SIZE) {
                        hasMore = false;
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onError() {
        super.onError();
        refreshLayout.setRefreshing(false);
        footerView.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        }
    }

    class DataListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data != null ? data.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHookItemView liveItemView = null;
            if (convertView == null) {
                liveItemView = new MyHookItemView(MyTvActivity.this);
                liveItemView.setOnLiveItemViewClickListener(new MyHookItemView.OnLiveItemViewClickListener() {
                    @Override
                    public void onClickLike(Hookup bean) {
                        AnchorDetailActivity.start(MyTvActivity.this, bean);
                    }
                });
                convertView = liveItemView;
            } else {
                liveItemView = (MyHookItemView) convertView;
            }
            liveItemView.setData(myhookupResponse.getType(), data.get(position));
            return convertView;
        }
    }
}
